package com.liush.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liush.model.controller.SysMenu;
import com.liush.common.helper.MenuHelper;
import com.liush.model.controller.SysRoleMenu;
import com.liush.model.vo.AssginMenuVo;
import com.liush.system.exception.GuiguException;
import com.liush.system.mapper.SysMenuMapper;
import com.liush.system.mapper.SysRoleMenuMapper;
import com.liush.system.service.SysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.org.apache.xalan.internal.xsltc.compiler.sym;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author liush
 * @since 2023-02-25
 */
@Service
@Transactional
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public List<SysMenu> findMenuNodes() {
        //1. 查询所有的菜单
        List<SysMenu> sysMenusAllList = sysMenuMapper.selectList(null);
        //2. 组装成树(倒着的树)
        List<SysMenu> treeList = MenuHelper.buildTree(sysMenusAllList);
        return treeList;
    }

    @Override
    public void delete(Long id) {
        Integer count = sysMenuMapper.getCountByParentId(id);
        if (count > 0) {
            throw new GuiguException(201, "这是父节点不能删除");
        } else {
            sysMenuMapper.deleteById(id);
        }
    }

    @Override
    public List<SysMenu> findSysMenuByRoleId(Long roleId) {
        /*//获取所有status为1的权限列表
        List<SysMenu> menuList = sysMenuMapper.selectList(new QueryWrapper<SysMenu>().eq("status", 1));
        //根据角色id获取角色权限
        List<SysRoleMenu> roleMenus = sysRoleMenuMapper.selectList(new QueryWrapper<SysRoleMenu>().eq("role_id", roleId));
        //获取该角色已分配的所有权限id
        List<Long> roleMenuIds = new ArrayList<>();
        for (SysRoleMenu roleMenu : roleMenus) {
            roleMenuIds.add(roleMenu.getMenuId());
        }
        //遍历所有权限列表
        for (SysMenu sysMenu : menuList) {
            if (roleMenuIds.contains(sysMenu.getId())) {
                //设置该权限已被分配
                sysMenu.setSelect(true);
            } else {
                sysMenu.setSelect(false);
            }
        }
        //将权限列表转换为权限树
        List<SysMenu> sysMenus = MenuHelper.buildTree(menuList);*/
        // 获取所有status为1的权限列表
        List<SysMenu> sysMenusList = sysMenuMapper.selectList(new LambdaQueryWrapper<SysMenu>().eq(SysMenu::getStatus, 1));
        // 根据角色id获取角色权限
        List<SysRoleMenu> sysRoleMenusList = sysRoleMenuMapper.selectList(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, roleId));
        // 获取该角色已分配的所有权限id集合
        List<Long> roleMenuIds = new ArrayList<>();
        sysRoleMenusList.forEach(sysRoleMenu -> {
            roleMenuIds.add(sysRoleMenu.getMenuId());
        });
        // 遍历所有权限列表
        sysMenusList.forEach(sysMenu -> {
            if (roleMenuIds.contains(sysMenu.getId())) {
                // 设置该权限已被分配
                sysMenu.setSelect(true);
            } else {
                sysMenu.setSelect(false);
            }
        });
        // 将权限列表转换为权限树
        List<SysMenu> sysMenus = MenuHelper.buildTree(sysMenusList);
        return sysMenus;
    }

    @Override
    public void doAssign(AssginMenuVo assginMenuVo) {
        Long roleId = assginMenuVo.getRoleId();
        // 删除该角色已分配的权限
        sysRoleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, roleId));
        // 遍历所有已选择的权限id
        assginMenuVo.getMenuIdList().forEach(menuId -> {
            if (menuId != null) {
                // 创建SysRoleMenu对象
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setRoleId(assginMenuVo.getRoleId());
                sysRoleMenu.setMenuId(menuId);
                // 添加新权限
                sysRoleMenuMapper.insert(sysRoleMenu);
            }
        });
    }
}